package cn.zkrt.iotms.module.iot.service.impl;

import cn.zkrt.iotms.module.iot.entity.DeviceWtblAttributeRelationB;
import cn.zkrt.iotms.module.iot.dao.DeviceWtblAttributeRelationBDao;
import cn.zkrt.iotms.module.iot.service.DeviceWtblAttributeRelationBService;
import cn.zkrt.iotms.module.iot.vo.DeviceWtblAttributeRelationBVO;
import cn.zkrt.iotms.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * 设备物通博联属性与设备属性关联表(DeviceWtblAttributeRelationB)表服务实现类
 *
 * @author xulinyu
 * @since 2024-10-09 16:08:13
 */
@Service("deviceWtblAttributeRelationBService")
public class DeviceWtblAttributeRelationBServiceImpl extends ServiceImpl<DeviceWtblAttributeRelationBDao,DeviceWtblAttributeRelationB> implements DeviceWtblAttributeRelationBService {
    @Resource
    private DeviceWtblAttributeRelationBDao deviceWtblAttributeRelationBDao;

    @Override
    public Page<DeviceWtblAttributeRelationBVO> queryByPage(Page<DeviceWtblAttributeRelationBVO> page, String deviceName, String typeAttributeName, String varName) {
        return deviceWtblAttributeRelationBDao.queryByPage(page,deviceName,typeAttributeName,varName);
    }

    @Override
    public Boolean add(DeviceWtblAttributeRelationB deviceWtblAttributeRelationB) {
        verifyDuplicate(deviceWtblAttributeRelationB);
        return this.save(deviceWtblAttributeRelationB);
    }

    @Override
    public Boolean edit(DeviceWtblAttributeRelationB deviceWtblAttributeRelationB) {
        verifyDuplicate(deviceWtblAttributeRelationB);
        return this.updateById(deviceWtblAttributeRelationB);
    }

    @Override
    public DeviceWtblAttributeRelationBVO queryById(String id) {
        return deviceWtblAttributeRelationBDao.queryById(id);
    }

    /**
     * 校验重复
     * @param deviceWtblAttributeRelationB deviceWtblAttributeRelationB
     */
    public void verifyDuplicate(DeviceWtblAttributeRelationB deviceWtblAttributeRelationB) {
        //不能重复
        LambdaQueryWrapper<DeviceWtblAttributeRelationB> w1 = new LambdaQueryWrapper<>();
        w1.ne(StringUtils.isNotEmpty(deviceWtblAttributeRelationB.getId()),DeviceWtblAttributeRelationB::getId,deviceWtblAttributeRelationB.getId());
        w1.eq(StringUtils.isNotEmpty(deviceWtblAttributeRelationB.getDeviceAttributeId()),DeviceWtblAttributeRelationB::getDeviceAttributeId,deviceWtblAttributeRelationB.getDeviceAttributeId());;
        int count = this.count(w1);
        if (count > 0) {
            throw new RuntimeException("属性配置已存在");
        }
    }
}
